package org.odk.clinic.android.activities;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.odk.clinic.android.R;
import org.odk.clinic.android.database.SQLiteAdapter;
import org.odk.clinic.android.openmrs1.concept_name;
import org.odk.clinic.android.openmrs1.drugs;
import org.odk.clinic.android.openmrs1.drugs_orders;
import org.odk.clinic.android.openmrs1.encounter;
import org.odk.clinic.android.openmrs1.observation;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public class NewObservationActivity extends Activity {
	private Button msave,mcancel;
	private EditText mdate,mcomment,munits,mquantity;
	private Spinner spinconcept,spindrugs,spintimes;
	private int spinerConceptId;
	private int mYear;
	private int mMonth;
	private int mDay;
	private drugs dr;
	private drugs_orders dror;
	private concept_name cname;
	private observation obs;
	private concept_name con_name;
	private encounter en;
	//
	private SQLiteAdapter mDB;
	private Cursor mCursor,mCursor1,mCursor2;
	java.text.DateFormat date = new SimpleDateFormat("dd-mm-yyyy");
	//
	protected void onCreate(Bundle ici){
		super.onCreate(ici);
		setContentView(R.layout.add_observation);
		////
		mDB = new SQLiteAdapter(this);
		mDB.open();
		////
		//set date current
				final Calendar c = Calendar.getInstance();
				mYear = c.get(Calendar.YEAR);
				mMonth = c.get(Calendar.MONTH);
				mDay = c.get(Calendar.DAY_OF_MONTH);
				mdate = (EditText)this.findViewById(R.id.obs_date);
				mdate.setText(new StringBuilder()
				.append(mDay).append("-")
				.append(mMonth).append("-")
				.append(mYear)
						
				);
				//
				en = new encounter();
				spintimes = (Spinner)this.findViewById(R.id.spin_obs_times);
				mCursor2 = mDB.getDataEncounter();
				startManagingCursor(mCursor2);
				String[] from1 = new String[]{SQLiteAdapter.KEY_ENCOUNTERNAME};
				int [] to1 = new int[]{R.id.text1};
				SimpleCursorAdapter da = new SimpleCursorAdapter(this,android.R.layout.simple_spinner_item,mCursor2,from1,to1);
				da.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
				spintimes.setAdapter(da);
				spintimes.setOnItemSelectedListener(new OnItemSelectedListener() {
				    	@Override 
				    	public void onItemSelected(AdapterView<?> parent,View view,int pos, long i){
				    		mCursor2 = (Cursor)parent.getItemAtPosition(pos);
				    	//	mCursor.getInt(mCursor.getColumnIndex(SQLiteAdapter.KEY_ENCOUNTERID));
				    		
				    	}
				    	@Override
				    	public void onNothingSelected(AdapterView <?>parent){
				    		
				    	}
				    	
					});
				//
				con_name = new concept_name();
				spinconcept  = (Spinner)this.findViewById(R.id.spin_concept);
			    mCursor = mDB.getConcept();
			    startManagingCursor(mCursor);
			    String[] from  = new String[]{SQLiteAdapter.KEY_CONCEPTNAME};
			    int[] to = new int[] {android.R.id.text1};
			    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,android.R.layout.simple_spinner_item,mCursor, from,to);
			    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			    spinconcept.setAdapter(adapter);
			    spinconcept.setOnItemSelectedListener(new OnItemSelectedListener() {
			    	@Override 
			    	public void onItemSelected(AdapterView<?> parent,View view,int pos, long i){
			    		mCursor = (Cursor)parent.getItemAtPosition(pos);
			    		spinerConceptId = mCursor.getInt(mCursor.getColumnIndex(SQLiteAdapter.KEY_CONCEPT_NAME_ID));
			    		
			    		
			    	}
			    	@Override
			    	public void onNothingSelected(AdapterView <?>parent){
			    		
			    	}
			    	
				});
						
				///
				spindrugs = (Spinner)this.findViewById(R.id.spin_drugs);
				ArrayAdapter adapter1 = ArrayAdapter.createFromResource(this, R.array.drugs, android.R.layout.simple_spinner_item);
				adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
				spindrugs.setAdapter(adapter1);
				//
				mcomment = (EditText)this.findViewById(R.id.obs_comments);
				munits = (EditText)this.findViewById(R.id.obs_units);
				mquantity = (EditText)this.findViewById(R.id.obs_quantity);
				msave = (Button)this.findViewById(R.id.btn_obs_save);				
				msave.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						//Toast.makeText(getApplicationContext(), "clicked", Toast.LENGTH_SHORT).show();
						
						dr = new drugs(); dror = new drugs_orders(); obs = new observation();
						//find patient_id
						int mid = ListPatientActivity2.mid;
						String patient = null;
						String encounter = null;
						mCursor = mDB.getPerson(mid);
						mCursor1 = mDB.getEncounter(mid);
						if(mCursor.moveToFirst()){
							
							patient = mCursor.getString(mCursor.getColumnIndex(SQLiteAdapter.KEY_GIVENNAME));
							int patient_id = mDB.find_patien_id(patient);
							obs.setObsPersonId(patient_id);
							
						}	
						if(mCursor1.moveToFirst()){
							encounter = 
									//"ADULTTINITIAL";
									mCursor1.getString(mCursor1.getColumnIndex(SQLiteAdapter.KEY_ENCOUNTERNAME));
							int encounter_id = mDB.find_encounter_id(encounter);
							obs.setObsEncounterId(encounter_id);
						}	
						
						
						
						try{
						obs.setObsDate(date.parse(mdate.getText().toString()));
						}
						catch(ParseException e)
						{
						e.printStackTrace();
							
						}
						
						
						obs.setObsConceptId(spinerConceptId);
						
						
						obs.setComment(mcomment.getText().toString());
						dr.setDrugsName(spindrugs.getSelectedItem().toString());
						dr.setUnit(munits.getText().toString());
						dr.setConceptId(spinerConceptId);
						dr.setQuantity(Integer.parseInt(mquantity.getText().toString()));
						mDB.createObservation(dr, dror, obs);
						setResult(RESULT_OK);
						finish();
						
						
					}
				});
					
					
			
	}
	

}
